package com.uep.controller;

import com.uep.entity.ActivityInformation;
import com.uep.service.ActivityInformationService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by XQ on 2017/2/9.
 */
@RestController
public class ActivityController {
    private static final Logger LOGGER = Logger.getLogger(ActivityController.class);
    @Autowired
    ActivityInformationService activityInformationService;

    /**
     * 获取所有活动的名称，时间，地点,状态的json数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "activities",method = RequestMethod.GET)
    public List<ActivityInformation> getNameTimeAddress()    {
        return activityInformationService.findNameTimeAddress();
    }

    /**
     * 获取活动详细信息并返回json数据
     * @param activityName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "activityDetails/activity/{activityName}",method = RequestMethod.GET)
    public ActivityInformation getActivityDetails(@PathVariable String activityName){
        return activityInformationService.findEventDetails(activityName);
    }

    /**
     * 获取简单的活动内容根据活动类型
     * @param activityType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "activityDetails/activityType/{activityType}",method = RequestMethod.GET)
    public List<ActivityInformation> getActivityByType(@PathVariable String activityType){
        return activityInformationService.findActivityType(activityType);
    }

    /**
     * 获取某个用户的所有活动的名称，时间和地点
     * @param userAccount
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "personActivity/userAccount/{userAccount}",method = RequestMethod.GET)
    public List<ActivityInformation> getPersonActivity(@PathVariable String userAccount){
        return activityInformationService.findPersonActivity(userAccount);
    }


    /**
     * 用户发起活动
     * @param activityInformation
     */
    @RequestMapping(value = "/activity",method = RequestMethod.POST)
    public void post4(@RequestBody ActivityInformation activityInformation){
        activityInformationService.insertOneActivity(activityInformation);
    }

    /**
     * 通过学校获取活动
     * @param schoolName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "schoolActivity/schoolName/{schoolName}",method = RequestMethod.GET)
    public List<ActivityInformation> getSchoolActivity(@PathVariable String schoolName){
        return activityInformationService.findSchoolActivity(schoolName);
    }

    /**
     * 获取首页的热门活动并且参加人数大于100
     * @param activityName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "hotActivity/activityName/{activityName}",method = RequestMethod.GET)
    public ActivityInformation getHotActivity(@PathVariable String activityName){
        return activityInformationService.findHotActivityByActivityStateAttendance(activityName);
    }

    /**
     * 查找参加人数
     * @param activityName
     * @return
     */
    @RequestMapping(value = "attendance/activityName/{activityName}",method = RequestMethod.GET)
    public ActivityInformation addAttendance(@PathVariable String activityName){
        return activityInformationService.findAttendance(activityName);
    }

    @RequestMapping(value = "/updateAttendance",method = RequestMethod.POST)
    public void updateAttendance(@RequestBody ActivityInformation activityInformation){
//        得到要更新的数据
        ActivityInformation activityInformation1 =
                activityInformationService.findAttendance(activityInformation.getActivityName());
//        设置更新后的数据
        activityInformation1.setAttendance(activityInformation.getAttendance());
        activityInformation1.setActivityName(activityInformation.getActivityName());
//        保存数据
        activityInformationService.updateActivityAttendance(activityInformation1);
    }
}
